package com.fm.controller;

import com.fm.entity.*;
import com.fm.service.*;
import com.fm.utils.KeyUtils;
import com.fm.utils.R;
import com.fm.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Key;
import java.util.*;

/**
 * Created by Administrator on 2018/7/30 0030.
 */
@RestController
@RequestMapping("/api/product")
public class ProductController extends MBaseController<Product, ProductService>{

    @Autowired
    ProductTopicService productTopicService;
    @Autowired
    SystemTopicService systemTopicService;
    @Autowired
    EquipmentService equipmentService;
    @Autowired
    ProductFunctionService productFunctionService;

    @Override
    public Product beforeList(Map t) {
        Product product = new Product();
        product.setUserId(SessionUtil.getLoginUserId());
        return product;
    }

    @Override
    public void beforeInsert(Product product, HttpServletRequest request) {
        product.setCreateTime(new Date());
        product.setUserId(SessionUtil.getLoginUserId());
        product.setProductKey(KeyUtils.getRandonString(11));
        product.setProductSecret(KeyUtils.getRandonString(16));
        product.setStatus(1);
    }

    @Override
    public void afterInsert(Product product, HttpServletRequest request) {
        //新增之后，新增topic
        List<SystemTopic> systemTopics = systemTopicService.selectByExample(new SystemTopic());
        for (SystemTopic systemTopic : systemTopics) {
            ProductTopic topic = new ProductTopic();
            topic.setIntroduction(systemTopic.getIntroduction());
            topic.setIsSystem(1);
            topic.setType(systemTopic.getType());
            topic.setValue(systemTopic.getValue().replace("productKey",product.getProductKey()));
            topic.setProductId(product.getId());
            productTopicService.insert(topic);
        }
        super.afterInsert(product, request);
    }

    @Override
    public void afterSelectOne(Product product) {
        Equipment equipment = new Equipment();
        equipment.setProductId(product.getId());
        product.setEqNumber(equipmentService.selectCount(equipment));
    }

    @RequestMapping("/getAll")
    public R getAll() {
        Product product = new Product();
        product.setUserId(SessionUtil.getLoginUserId());
        List<Product> products = baseService.selectByExample(product);
        return R.ok().put("data", products);
    }

    @RequestMapping("/getDetailValues")
    public R getDetailValues(Integer id) {
        Equipment equipment = new Equipment();
        equipment.setProductId(id);
        List<Equipment> equipments = equipmentService.selectByExample(equipment);
        ProductTopic topic = new ProductTopic();
        topic.setProductId(id);
//        topic.setType(1);
        List<ProductTopic> topics = productTopicService.selectByExample(topic);
        List<ProductFunction> functions = productFunctionService.selectParamAndMetodByProductId(id);
        Map<String, Object> reMap = new HashMap<>();
        reMap.put("equipments", equipments);
        reMap.put("params", functions);
        //获取所有的事件
        ProductFunction function = new ProductFunction();
        function.setProductId(equipment.getProductId());
        function.setType(3);
        List<ProductFunction> event = productFunctionService.selectByExample(function);
        List<ProductTopic> reTopics = new ArrayList<>();
        for (ProductTopic t : topics) {
            if (t.getValue().contains("${tsl.event.identifer}")){
                for (ProductFunction f : event){
                    t.setValue(t.getValue().replace("${tsl.event.identifer}", f.getName()));
                    reTopics.add(t);
                }
            }else {
                reTopics.add(t);
            }
        }
        reMap.put("topics", reTopics);
        return R.ok().put("data", reMap);
    }

    @RequestMapping("/getProductByKey")
    @ResponseBody
    public R getProductByKey(String productKey){
        Product product = new Product();
        product.setProductKey(productKey);
        List<Product> products = this.baseService.selectByExample(product);
        return R.ok().put("data", products.get(0));
    }
}
